﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using Common;
using System.Web.UI.WebControls;
using System.Globalization;

namespace ProjectDan.CRM.Views
{
	public partial class ListUploadedFiles : Microsoft.Practices.CompositeWeb.Web.UI.Page, IListUploadedFilesView
	{
		private ListUploadedFilesPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public ListUploadedFilesPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        void repUploadedFiles_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            UploadedFile uploadedFile = e.Item.DataItem as UploadedFile;

            HyperLink hylUploadedFile = e.Item.FindControl("hylUploadedFile") as HyperLink;

            hylUploadedFile.Text = uploadedFile.Name;

            hylUploadedFile.NavigateUrl = string.Format(CultureInfo.InvariantCulture, "EditUploadedFile.aspx?File={0}", uploadedFile.Id);

            hylUploadedFile.ToolTip = uploadedFile.Description;
        }

        public void Show(IEnumerable<UploadedFile> uploadedFiles)
        {
            repUploadedFiles.ItemDataBound+=new RepeaterItemEventHandler(repUploadedFiles_ItemDataBound);
            
            repUploadedFiles.DataSource = uploadedFiles;
            
            repUploadedFiles.DataBind();

            repUploadedFiles.ItemDataBound -= repUploadedFiles_ItemDataBound;
        }
    }
}

